package com.liuwei.desktop.view;

import java.awt.Container;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import com.liuwei.desktop.common.AWTUtils;
import com.liuwei.desktop.common.ExceptionUtils;
import com.liuwei.desktop.modal.entity.TUser;
import com.liuwei.desktop.modal.repository.UserService;
import com.liuwei.desktop.modal.repository.impl.UserServiceImpl;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class SearchView {

	private JFrame frame;
	private JTextField textField;
	private JTable table;
	private JScrollPane scrollPane;
	private UserService userService = new UserServiceImpl();

//	/**
//	 * Launch the application.
//	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					SearchView window = new SearchView();
//					window.frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the application.
	 * @throws Exception 
	 */
	public SearchView(){
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * @throws Exception 
	 */
	private void initialize(){
		frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.getContentPane().setLayout(null);
        
        JLabel lblNewLabel = new JLabel("查询字符");
        lblNewLabel.setBounds(33, 26, 54, 15);
        frame.getContentPane().add(lblNewLabel);
        
        textField = new JTextField();
        textField.setBounds(136, 23, 484, 21);
        frame.getContentPane().add(textField);
        textField.setColumns(10);
		textField.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e) {
				if ( e.getKeyCode() == KeyEvent.VK_ENTER){
					try {
						System.out.println(Thread.currentThread().getName());
	        			String filter = textField.getText();
						DefaultTableModel tableModel;
						tableModel = buildModel(userService.getNameLikeUser(filter));
						AWTUtils.refreshTable(table,tableModel);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}
		});
        
        JButton btnNewButton = new JButton("搜索");
        btnNewButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		try {
        			String filter = textField.getText();
					DefaultTableModel tableModel = buildModel(userService.getNameLikeUser(filter));
					AWTUtils.refreshTable(table,tableModel);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
        	}
        });
        btnNewButton.setBounds(652, 22, 93, 23);
        frame.getContentPane().add(btnNewButton);

		try {
			DefaultTableModel tableModel;
			tableModel = buildModel(userService.getAllUser());
	        table = new JTable(tableModel);
	        
	        scrollPane = new JScrollPane(table);
	        scrollPane.setBounds(33, 72, 712, 480);
	        frame.getContentPane().add(scrollPane);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	private DefaultTableModel buildModel(List<TUser> users){
		String[] Names = { "编号", "名称", "描述" };
		if(users!=null && users.size()>0){
			int userSize = users.size();
			Object[][] data = new Object[userSize][3];
			for(int i=0;i<userSize;i++){
				TUser u = users.get(i);
				Object[] row = {u.getPkid()+"",u.getName(),u.getTel()};
				data[i] = row;
			}
			DefaultTableModel tableModel = new DefaultTableModel(data,Names);
			return tableModel;
		}
		return null;
	}
	
	public JFrame getFrame(){
		return this.frame;
	}
}
